package com.demo.task.download

inline fun DownloadManager.addDownloadListener(
    crossinline begin: (String) -> Unit = {},
    crossinline complete: () -> Unit = {},
    crossinline fileComplete: (url: String) -> Unit = {},
    crossinline prog: (progress: Long) -> Unit = {},
    crossinline failure: (url: String, t: Throwable) -> Unit = { _, _ -> }
): DownloadCallback {
    val downloadCallback = object : DownloadCallback {
        override fun begin(url: String) {
            begin.invoke(url)
        }

        override fun complete() {
            complete.invoke()
        }

        override fun fileComplete(url: String) {
            fileComplete.invoke(url)
        }

        override fun progress(progress: Long) {
            prog.invoke(progress)
        }

        override fun failure(url: String, t: Throwable) {
            failure.invoke(url, t)
        }
    }
    listener = downloadCallback
    return downloadCallback
}